package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "新增菜品")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    //根据分类id查询菜品
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> selectCatGoryById(Long id) {
        //打印
        log.info("[分类id]id:{}",id);
        //调用
        List<Dish> dishList = dishService.selectCateGoryById(id);
        return Result.success(dishList);
    }

    //需求：保存更新
    @PutMapping
    @ApiOperation("更新菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("[更新菜品] dishDto:{}",dishDTO);
        dishService.updateDishById(dishDTO);
        // 清除缓存
        clearRedisByCategoryId("dish:"+dishDTO.getCategoryId());
        return Result.success();
    }

    //需求:修改菜品，先回显
    @GetMapping("/{id}")
    public Result<DishVO> findById(@PathVariable Long id) {

        log.info("[基于ID查询] id:{}", id);
        DishVO dishVo = dishService.findById(id);
        return Result.success(dishVo);
    }

    // 需求：批量删除菜品
    //@RequestParam 目的:前端传递是/xx/dish?ids=a,b,c,d,e
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids){
        log.info("[批量删除] ids:{}",ids);
        dishService.deleteDishByIds(ids);
        // 全部删除
        clearRedisByCategoryId("dish:*");
        return Result.success();
    }


    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        //打印
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        //调用
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 新增菜品
     *
     * @param dishDTO
     */

    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO) {

        log.info("[新增菜品] dishDto:{}",dishDTO);
        // 调用
        dishService.addFlavor(dishDTO);

        // 新增菜品后，需要清空缓存
        String key = "dish:"+dishDTO.getCategoryId();
        clearRedisByCategoryId(key);
        // 响应
        return Result.success();
    }

    private void clearRedisByCategoryId(String keyPattern) {
        // 先根据key表达式去redis搜索相关key
        // dish:1 dish:2
        Set<String> keys = redisTemplate.keys(keyPattern);
        if (keys!=null){
            // 删除
            redisTemplate.delete(keys);
        }
    }
}
